<?php
namespace app\massage\model;

use app\BaseModel;
use app\farm\model\BreedOrderGoods;
use app\farm\model\ClaimOrder;
use app\farm\model\LandOrder;
use think\facade\Db;

class BalanceWater extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_balance_water';

//
    protected $append = [

        'goods_title',

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-07 17:44
     * @功能说明:项目名称
     */
    public function getGoodsTitleAttr($value,$data){

        if(!empty($data['type'])&&!empty($data['order_id'])&&isset($data['add'])){

            $balance_order_model = new BalanceOrder();

            //
            if($data['type']==1){

                $title = $balance_order_model->where(['id'=>$data['order_id']])->value('title');
            //认养
            }elseif($data['type']==2){

                $order_model = new ClaimOrder();

                $title = $order_model->where(['id'=>$data['order_id']])->column('goods_name');
            //养殖
            }elseif($data['type']==3){

                $shop_order_model = new BreedOrderGoods();

                $title = $shop_order_model->where(['order_id'=>$data['order_id']])->column('goods_name');

                $title = !empty($title)?implode(',',$title):'';
            //土地
            }elseif($data['type']==4){

                $shop_order_model = new LandOrder();

                $title = $shop_order_model->where(['id'=>$data['order_id']])->column('massif_title');

            }

            return $title;

        }


    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

//        if(){
//
//        }

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function indexList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $add_text = $v['add']==1?'+':'-';

                $buy_text = $v['add']==1?'退款':'消费';

                if($v['type']==1){

                    $buy_text = '';
                }

                $scene_text = $this->sceneText($v['type']);

                $v['text'] = $scene_text.$buy_text.'【'.$v['goods_title'].'】'.$add_text.'¥'.$v['price'].'现余额¥'.$v['after_balance'];


            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-05 09:56
     * @功能说明:
     */
    public function sceneText($type){

        switch ($type){

            case 1:

                $text = '购买储值卡';

                break;
            case 2:

                $text = '认养订单';

                break;

            case 3:

                $text = '养殖订单';

                break;

            case 4:

                $text = '土地订单';

                break;

            default:

                $text = '购买储值卡1';

                break;

        }

        return $text;

    }





    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}